// pages/order/review.js
import { reviewsOrder } from '../../utils/apis'

import { alert } from '../../utils/util'

var RATE_LABELS = {
  1: '非常差',
  2: '很差',
  3: '一般',
  4: '很好',
  5: '非常好'
}

Page({
  /**
   * 页面的初始数据
   */
  data: {
    RATE_LABELS,
    limitLength: 8,
    content: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.id = options.id
  },
  onChangeTime(e) {
    var reach_time = e.detail.value
    this.setData({
      reach_time
    })
  },
  onServiceChange(e) {
    var { id: service } = e.currentTarget
    this.setData({
      service
    })
  },
  onQualityChange(e) {
    var { id: quality } = e.currentTarget
    this.setData({
      quality
    })
  },
  onContentInput(e) {
    var { value: content } = e.detail
    this.setData({
      content
    })
  },
  onTasteChange(e) {
    var { id: taste } = e.currentTarget
    this.setData({
      taste
    })
  },
  onSubmit(e) {
    var that = this
    var { id: order_id } = this
    console.log('orderid:', order_id)
    var { limitLength, service, quality, content, taste } = this.data
    if (!taste) {
      return alert('请为口味打分')
    }
    if (!quality) {
      return alert('请为包装打分')
    }
    if (!service) {
      return alert('请为配送打分')
    }
    if (content.length < limitLength) {
      return alert(`评论内容不能小于${limitLength}个字`)
    }

    this.setData({
      loading: true
    })
    reviewsOrder({
      order_id,
      service,
      quality,
      taste,
      content
    })
      .then(data => {
        that.setData({
          loading: false
        })
        alert('评论成功', function() {
          wx.navigateBack()
        })
      })
      .catch(err => {
        alert(err.message, function() {
          wx.navigateBack()
        })

        that.setData({
          loading: false
        })
      })
  }
})
